package com.zhkj.txg.module.mine.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.mine.entity.DeleteArticleResponse
import com.zhkj.txg.module.mine.entity.PublishListResponse
import com.zhkj.txg.module.mine.model.MineManager

class PublishViewModel(application: Application) : BaseViewModel(application) {
    private val manager = MineManager()
    val publishListLiveData = MutableLiveData<HttpResponse<PublishListResponse>>()
    val deleteArticleLiveData = MutableLiveData<HttpResponse<DeleteArticleResponse>>()

    private var page = 1
    private val numb = 20

    fun getPublishList(restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<PublishListResponse>(publishListLiveData) {
            override fun onSuccess(resp: PublishListResponse) {
                resp.hasMore = resp.data.size > numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val articleList = manager.publishArticleList(AccountManager.getToken(), page, numb, listener)
        dispose.add(articleList)
    }

    fun deleteArticle(id: Long) {
        val listener = ViewModelSubscribeListener(deleteArticleLiveData)
        val deletePublishArticle = manager.deletePublishArticle(AccountManager.getToken(), id, listener)
        dispose.add(deletePublishArticle)
    }
}